<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of activityad
 *
 * @author dragon.hu add @2016.5.9
 */
defined('BASEPATH') or die('forbidden');

class Activityad_Controller extends Controller {
    private $strategy_array;
    private $allad;
    private $areanum;
    
    public function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        //$this->strategy_array = array('lastarea'=>  translate('GENERAL_STRATEGY_LASTAREA'),'randarea' => translate('GENERAL_STRATEGY_RANDAREA'));
        
        // 得到选择的区号
        $this->areanum = $_SESSION ['area_selected_id'];
        if ($this->areanum == '') {
        	echo "<script>alert('" . translate ( 'C_SELECT_AREA_TEXT' ) . "');</script>";
        	return;
        }
        $config = get_config();
        $this->allad = $config['inc']['activityad'];
        //var_dump($this->allad);
        //exit();
    }

    function index() {
        $this->view->display("activity/activityad/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        
        $sql = "SELECT COUNT(*) AS num FROM ad_manage WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder}" : "";
        $this->db->query("SELECT {$colsinfo} FROM ad_manage WHERE {$where} {$orderby} LIMIT {$offset},{$rp}");
        while ($n = $this->db->get_array('default')) {
        	$n['adname'] = $this->allad[$n['adname']];
            $rows[] = array('id' => $n['id'], 'cell' => $n);
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

function add()
	{
		$tmparr = array();
		$sql = "SELECT * FROM ad_manage";
		$this->db->query($sql);
		while($n = $this->db->get_array('default'))
		{
			$tmparr[] = $n;
		}
		
		if (is_submit())
		{
			$rowdata = $_POST['row'];
			//var_dump($_POST);
			//exit();
			for ($i = 0; $i < count($tmparr); $i++)
			{
				if ($tmparr[$i]['adname'] == $rowdata['ad'])
				{
					echo "<script>alert('name can not repeat !!!');</script>";
					$this->view->assign ("ad", $this->form->select ("row[ad]", $this->allad, '', array('id' => 'ad')));
					$this->view->display("activity/activityad/add");
					return;
				}
			}
			
			//vpost
			$arealist = get_area_list_live();
			foreach ($arealist as $k => $v)
			{
				$result = vpost($v['host']."adname=".$rowdata['ad']."&sequence=".$rowdata['sequence']."&status=".$rowdata['status']."&operation=1"."&action=56");
			}
			if ($result != '0' || $result === FALSE)  //多个返回的情况如何处理？
			//if (0)
			{
				cp_tips("error");
			}
			else
			{
				//2.加入数据库中
				$manager = $this->auth->fields['username'];
				$addtime = date('Y-m-d H:i:s', time());
				$sql = "INSERT INTO ad_manage(adname, sequence, status, manager, addtime) VALUES ('".$rowdata['ad']."', ".$rowdata['sequence'].", ".$rowdata['status'].", '".$manager."','".$addtime."')";
				if($this->db->execute($sql))
				{
					insert_log(translate('MENU_ACTIVITY_AD'),translate('EDIT'), NULL, array('adname' => $rowdata['adname']));
					cp_tips("success");
				}
			}
		}
		$this->view->assign ("ad", $this->form->select ("row[ad]", $this->allad, '', array('id' => 'ad')));
		$this->view->display("activity/activityad/add");
	}

	function del($id = 0)
	{
		$c = 0;
		$r = '';
		if ($id === 0)
			json_output($c, translate('C_INVALID_PARAM_ID', $id));
		$row = $this->db->one("SELECT * FROM ad_manage WHERE id=$id");
		if (!is_array($row))
			json_output($c, translate('C_INVALID_PARAM_ID', $id));
		
		$arealist = get_area_list_live();
		foreach ($arealist as $k => $v)
		{
			$result = vpost($v['host']."adname=".$row['adname']."&sequence=".$row['sequence']."&operation=0"."&action=56");
		}

		if ($result != '0' || $result === FALSE)
		//if (0)
		{
			$c = 1;
		}
		else
		{
			if ($this->db->delete('ad_manage', array('id' => $id))) {
				insert_log(translate('MENU_ACTIVITY_AD'),translate('DEL'), array('id'=> $id));
				$c = 1;
			} else {
				$r = translate('C_TIPS_ERROR_TEXT');
			}
		}
		json_output($c, $r);
	}
    
}
